#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "study_define.h"
#include "other.h"

#include "sort.h"
#define ADJUST 7.31
typedef double real;

#include "struct_study.h"


extern int giantxs;
static int dodgers = 3;

#define SQUARE(x) ((x)*(x))
#define PR(x) printf("#define SQUARE(%d) =%d\n",x,SQUARE(x)) //预处理器不做运算，不求值，只替换字符序列。函数调用和宏函数是有区别的，
#define PR2(x) printf("#define SQUARE("#x") =%d\n",SQUARE(x)) 

double power(double n,int p);
int main(void){

   color_count();
   const double SCALE = 0.333;
   double shoe,foot;
   printf("Shoe size (men's)    foot length\n");
   shoe = 9.0;
   while (shoe <19.0)
   {
     foot = SCALE *shoe +ADJUST;


     
     
     printf("%10.1f %15.2f inches \n",shoe,foot);
     shoe = shoe+1.0;

     printf("size of foot %zd bytes!",sizeof foot);//size of 返回size_t 
      
     int a =1;
     int q =0;
     int q2 =0;
     q = 2*++a;//首先，a递增1；然后，2乘以a，并将结果赋给q
     q2 = 2*a++;//首先，2乘以a，并将结果赋给q2,；然后，a递增1
     
     char ch = {0};

     

   }
   real yu = 1.0;
   printf("类型别名：yu=%0.1f\n",yu);

  _Bool input_is_good;
  long num =0;
  printf("Please input a num:");
  input_is_good = (scanf("%ld",&num) == 1);

  while (input_is_good){
    printf("intpu num %ld \n",num);
    printf("Please input next num (q to qiut):");
    input_is_good = (scanf("%ld",&num) == 1);

  }
  //清空输入缓存，以免影响后续输入
  int chs;

  while ((chs = getchar()) != '\n')
  {
     printf("you input num is %c \n",chs);
  }
  
  
  printf("intput over!!!!\n");

  const int ROWS_ =6;
  const int CHARS = 6;
  int row;
  char ch;

  for (row =0; row < ROWS_; row++)
  {
    for (ch = ('A'+row); ch < ('A'+CHARS); ch++)
    {
      printf("%c",ch);
    }
    printf("%c is alpha %d\n",ch,isalpha(ch));
  }
  

  printf("%f",power(6.28,3));

  showPrice(6.28,3);

  int x=5,y=10;
  printf("Origin x=%d,y=%d\n",x,y);

  interchange2(&x,&y);

  printf("after interchange2 x=%d,y=%d\n",x,y);
  interchage1(x,y);
  printf("after interchange1 x=%d,y=%d\n",x,y);
  printf("x=%d,y=%d\n",x,y);


  int arr[] = {1,2,3,4,5};
  int n = sizeof arr / sizeof arr[0];
  printf("sum=%d\n",sum(arr,n));
  test();

  int ar[3][COLS] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
  // sum_rows(ar,ROWS);//报错 因为ar的列数和函数声明的5不符合
  printf("sum=%d\n",vla_sum2(3,4,ar));

  findWords();


  // test_sort();

    char input[LIMT][SIZE];
     char *ptstr[LIMT];

     int input_count =0;
     int output_count;

     printf("Input up to %d lines,and I will sort them.\n",LIMT);
     printf("to stop,press the Enter key at a line's start.\n");

     while (input_count <LIMT  && s_gets(input[input_count],SIZE) !=NULL && input[input_count][0]!='\0')
     {
          ptstr[input_count] = input[input_count];//设置指针指向字符串
          input_count ++;
     }
     

     sort_str(ptstr,input_count);

     printf("Here are the sorted strings:\n");
     for (output_count  = 0; output_count < input_count; output_count++)
     {
          puts(ptstr[output_count]);

     }

     int entity = 3;
     int * ptrr = &entity;
     int ranks[10]={1,2,3,4,5,6,7,8,9,10};
     *(ranks+2*entity) = 77;//有效的左值表达式，指向ranks的第7个元素

     printf("ranks[6]=%d\n",ranks[6]);

     const char * pc = "string constant";//const 只能保证被pc指向的字符串内容不被修改，但是无法保证pc不指向别的字符串


     // 链接、作用域
     printf("外部链接a giantxs=%s\n","s");
     printf("外部链接 giantxs=%d\n",giantxs);

     //文件操作
     int cher;
     FILE *fp;
     FILE *fp_out;

     fp = fopen("test.txt","r");
     if ((fp_out=fopen("test_out.txt","a+")) == NULL)
     {
      fprintf(stderr,"Can't open output file.\n");
      exit(3);
     }
      
      
      putc('-',fp_out);
     while ((cher = getc(fp))!= EOF)
     {
      rewind(fp_out);
      putc(cher,fp_out);
      // putchar(cher);
     }
     fclose(fp);
     fclose(fp_out);
     printf("file close\n");


   struct funds stan = {
        "GGG",
        4032.55,
        "Lucy",
        5636.85
   };

  struct funds stan2 = {
        "GGG",
        4032.55,
        "Lucy",
        5636.84
   };

   stan = stan2;//结构的赋值

  printf("struct sum=%.2f",sum_struct(&stan));
  printf("struct bankfund=%.2f",changeName(stan).bankfund);

   struct funds_p fundsp = {
        "qqq",
        4032.55,
        "dg",
        5636.84
   };
  getstruct_p_info(&fundsp);
  printf("struct funds_p.bank =%s,save=%s\n",fundsp.bank,fundsp.save);
  cleanup(&fundsp);

  //枚举
  enum spectum {red,orange,yellow,blue};
  enum spectum colors;//C++中可以不写关键字enum

  // printf("#define SQUARE(5) =%d\n",SQUARE(5));
  PR(6);//类函数宏
  PR2(7);
  // test_defined();

    int *hope_p;
    int hope= 126;
    printf("hope>>变量的地址=%p\n", &hope);
    printf("hope_p>>变量的地址=%p\n", &hope_p);
    printf("---------------------------------------\n");
    hope_p =&hope;
    printf("hope_p的值=%d\n", *hope_p);
    printf("hope_p>变量的地址=%p\n", (void*)&hope_p);
    // printf("hope_p变量的地址=%p\n", &hope_p);
    printf("hope_p>>变量的地址=%p\n", &hope_p);
    printf("hope_p>>>变量的地址=%p\n", hope_p);
    printf("hope变量的地址=%p\n", &hope);
    *hope_p =10;//指针前面加* 表示变量，和hope是一样的
    printf("hope=%d\n",hope);

  return 0;
}

double power(double n,int p){
  double pow =1;
  for (int i = 0; i < p; i++)
  {
    pow *= n;
  }
  return pow;
}